import { Base } from '../../utils/base.js';
const {
  business,
  businesscollect
} = require('../../utils/api')
const { fetch } = require('../../utils/fetch')
var base = new Base();
const app = getApp()

//#endregion
// 

Page({
  data:{
    value:'',
    id:'',
    data:{},
    imgs:[],
    reply_text:'',
    reply_list:[],
    is_collect: true,
    in_type:'',
    isEdit:true,
    specificarea:null,
  },
  onLoad: function (options) {
    if(wx.getStorageSync('specificarea')) {
      this.setData({
        specificarea:wx.getStorageSync('specificarea')
      })
    }

    if (options.in_type=='my'){
      this.setData({
        in_type: options.in_type
      })

      //获取用户商户
      this.initMerchant();
      
    }else{

      this.setData({
        in_type:'',
        id: options.id,
      })
      this.initData();
    } 
    
  },
  initMerchant(){
    var that = this;
    let userInfo = wx.getStorageSync('userInfo');

    //商户详情 
    var param = {
      url: `store/${this.data.specificarea}/mylist`,
      data: {
        "memberId": userInfo["id"]
      },
      sCallback: function (res) {
        
          if(!res){
            base.showmsg('您未发布您的商户');
            setTimeout(function(){
              wx.navigateBack({
                delta: 1
              })
            },2000);
          }else{
            var my=res.list[0];
            that.setData({
              id:my.id
            });
            that.initData();
          }
      
      }
    };
    base.request(param);
  },
  initData(){
    var that = this;
    fetch(`wx/store/${this.data.specificarea}/detail`, 'POST', {
      id: that.data.id
    }).then(res => {
      this.setData({
        data: res,
        imgs: res.store_img.split(',')
      })

    })

  },

  onChange:function(e){
    this.data.reply_text = e.detail.value;
  },
  bind_reply_submit:function(){
    var reply_text=this.data.reply_text;
    if(reply_text==''){
      base.showmsg('请输入评论');
      return;
    }
    let userInfo = wx.getStorageSync('userInfo');
    var pardata={
      'content': reply_text,
      'memberid': userInfo["id"],
      'type':2,
      'typeid': this.data.id,
      'area':this.data.specificarea
    }
    var that = this;
    var param = {
      url: `reply/${this.data.specificarea}/fabu`,
      data: pardata,
      sCallback: function (data) {
        base.showmsg('评论成功');
        that.setData({
          'reply_text':''
        });
        //评论列表
        // that.get_reply_list();
      }
    };
    base.request(param);
  },
  onClickCollect: function (e) {
    var that = this;
    if(!wx.getStorageSync('userInfo')) {
      wx.reLaunch({
          url: '/pages/login/index'
      })
      return;
  }
  let userInfo = wx.getStorageSync('userInfo');

    var param = {
      url: `store/${this.data.specificarea}/collect`,
      data: {
        // "area": this.data.specificarea,
        'storeId': this.data.id,
        'memberId': userInfo["id"]
      },
      sCallback: function (data) {
        that.setData({
          is_collect: false
        });
        base.showmsg('已收藏');
      }
    };
    base.request(param);
  },
  scollect(event){
    fetch(`wx/pay/${this.data.specificarea}/collectlist`, 'POST', {
      memberId: this.data.id,
  }).then(res => {
      this.setData({
        data:res,
        imgs:res.store_img.split(',')
    })
    })
  },
  tel:function (event) {
    wx.makePhoneCall({
      phoneNumber: this.data.data.store_tel,
    })
  },
  previewimg(options){
    let {currentTarget} = options.currentTarget.dataset
    wx.previewImage({
      current: currentTarget,
      urls: this.data.imgs
  })
  },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.data.store_name,
      path: '/pages/businessdetails/index/index?id=' + this.data.id,
      imageUrl: this.data.data.logo
    }
  },
  // // 编辑商户
  // goEditmerchants:function(){
  //   this.setData({
  //     isEdit:false,

  //   })
  // },
  
})
